{{! ===================================================== }}
{{! 序列化器模板 (CoarseSerializer)                    }}
{{! 特点：性能优先，直接对象访问，简化默认值处理                  }}
{{! ===================================================== }}

{{! 序列化名称常量 }}
const {{class.className}}_SERIAL_NAME = '{{serialNameConstant}}';

{{#if class.generics.isGeneric}}
{{! 泛型类：工厂模式 }}
export class {{class.className}}Serializer<{{genericParametersString class.generics.parameters}}> extends Serializer<{{class.className}}<{{genericParametersString class.generics.parameters}}>> {
  readonly serialName: string = {{class.className}}_SERIAL_NAME;

  {{! 属性序列化器声明 }}
  {{#each properties}}
    {{#unless (eqPrimitiveType analysis.type.kind)}}
    private readonly {{analysis.name}}Serializer: {{serializer.typeDeclaration}};
    {{/unless}}
  {{/each}}

  readonly serialDescriptor: SerialDescriptor;

  constructor(
    {{#each class.generics.parameters}}
    {{toLowerCase this}}Serializer: Serializer<{{this}}>{{#unless @last}},{{/unless}}
    {{/each}}
  ) {
    super();
    {{! 初始化属性序列化器 }}
    {{#each properties}}
      {{#unless (eqPrimitiveType analysis.type.kind)}}
      this.{{analysis.name}}Serializer = {{serializer.instantiationTemplate}};
      {{/unless}}
    {{/each}}

    this.serialDescriptor = buildClassSerialDescriptor(
      {{class.className}}_SERIAL_NAME,
      builder => {
        {{#each properties}}
          {{! 处理数字属性名的特殊情况 }}
          {{#if (isNumericPropertyName (formatPropertyName analysis))}}
          builder.element({
            elementName: '{{formatPropertyName analysis}}',
            elementDecodingName: this.fixNonNegativeIntegerElementName('{{formatPropertyName analysis}}'),
            descriptor:
              {{#if (eqPrimitiveType analysis.type.kind)}}
                {{serializer.name}}.INSTANCE.serialDescriptor
              {{else}}
                this.{{analysis.name}}Serializer.serialDescriptor
              {{/if}}
          });
          {{else}}
          builder.element({
            elementName: '{{formatPropertyName analysis}}',
            descriptor:
              {{#if (eqPrimitiveType analysis.type.kind)}}
                {{serializer.name}}.INSTANCE.serialDescriptor
              {{else}}
                this.{{analysis.name}}Serializer.serialDescriptor
              {{/if}}
          });
          {{/if}}
        {{/each}}
      });
  }
{{else}}
{{! 非泛型类：单例模式 }}
export class {{class.className}}Serializer extends Serializer<{{class.className}}> {
  private static _INSTANCE?: {{class.className}}Serializer;
  readonly serialName: string = {{class.className}}_SERIAL_NAME;

  static get INSTANCE(): {{class.className}}Serializer {
    if (!{{class.className}}Serializer._INSTANCE) {
      {{class.className}}Serializer._INSTANCE = new {{class.className}}Serializer();
    }
    return {{class.className}}Serializer._INSTANCE;
  }

  {{! 属性序列化器声明 }}
  {{#each properties}}
    private readonly {{analysis.name}}Serializer: {{serializer.typeDeclaration}} = {{serializer.instantiationTemplate}};
  {{/each}}

  {{! 序列描述符构建 }}
  readonly serialDescriptor: SerialDescriptor = buildClassSerialDescriptor(
      {{class.className}}_SERIAL_NAME,
      builder => {
        {{#each properties}}
          {{! 处理数字属性名的特殊情况 }}
          {{#if (isNumericPropertyName (formatPropertyName analysis))}}
          builder.element({
            elementName: '{{formatPropertyName analysis}}',
            elementDecodingName: this.fixNonNegativeIntegerElementName('{{formatPropertyName analysis}}'),
            descriptor: this.{{analysis.name}}Serializer.serialDescriptor
          });
          {{else}}
          builder.element({
            elementName: '{{formatPropertyName analysis}}',
            descriptor: this.{{analysis.name}}Serializer.serialDescriptor
          });
          {{/if}}
        {{/each}}
      }
    )

  private constructor() {
    super();
  }
{{/if}}

  {{! 序列化方法：返回ESObject }}
  serialize(encoder: Encoder, value: {{#if class.generics.isGeneric}}{{class.className}}<{{genericParametersString class.generics.parameters}}>{{else}}{{class.className}}{{/if}}): ESObject {
    {{#if (canReturnDirectly class)}}
    {{! 性能优化：只包含基础类型时直接返回对象 }}
      if (encoder.allowSourceReuse()) {
        return value;
      }
      return this.innerSerialize(encoder, value);
    {{else}}
      return this.innerSerialize(encoder, value);
    {{/if}}
  }

  private innerSerialize(encoder: Encoder, value: {{#if class.generics.isGeneric}}{{class.className}}<{{genericParametersString class.generics.parameters}}>{{else}}{{class.className}}{{/if}}): ESObject {
    const keys = this.serialDescriptor.elementNames;
    const result = encoder.newObject();
    {{#each properties}}
    {{! 根据类型选择编码方法 }}
      {{#if (eq analysis.visibility "private")}}
        {{#if (and (eqPrimitiveType analysis.type.kind) (not (hasDecoratorWith analysis.decorators)))}}
          encoder.{{serializer.encodeMethod}}(result, keys[{{@index}}], ObjectUtils.getProp(value, '{{analysis.name}}'));
        {{else}}
          encoder.encodeSerializableElement(result, keys[{{@index}}], this.{{analysis.name}}Serializer, ObjectUtils.getProp(value, '{{analysis.name}}'));
        {{/if}}
      {{else}}
        {{#if (and (eqPrimitiveType analysis.type.kind) (not (hasDecoratorWith analysis.decorators)))}}
          encoder.{{serializer.encodeMethod}}(result, keys[{{@index}}], value.{{analysis.name}});
        {{else}}
          encoder.encodeSerializableElement(result, keys[{{@index}}], this.{{analysis.name}}Serializer, value.{{analysis.name}});
        {{/if}}
      {{/if}}
    {{/each}}

    return result;
  }

  {{! 动态包含反序列化模板 - 使用从上下文传递的deserializeTemplate参数 }}
  {{> (lookup . "deserializeTemplate")}}
}
{{#if class.generics.isGeneric}}
{{! 基于 BaseSerializerFactory 的工厂类 }}
export class {{class.className}}SerializerFactory extends BaseSerializerFactory<{{class.className}}<{{genericParametersESObjString class.generics.parameters}}>> {

  protected getSerialName(): string {
    return {{class.className}}_SERIAL_NAME;
  }

  protected getRequiredSerializersCount(): number {
    return {{class.generics.parameters.length}};
  }

  protected newSerializer(genericArgumentsSerializers: Serializer<ESObject>[]): Serializer<{{class.className}}<{{genericParametersESObjString class.generics.parameters}}>> {
    return new {{class.className}}Serializer<{{genericParametersESObjString class.generics.parameters}}>(
      {{#each class.generics.parameters}}
      genericArgumentsSerializers[{{@index}}]{{#unless @last}},{{/unless}}
      {{/each}}
    );
  }

  protected getSerializerName(): string {
    return {{class.className}}Serializer.name;
  }
}
{{/if}}

{{#if (isClassType class)}}
{{#if class.generics.isGeneric}}
{{! 工厂注册 }}
registerAutoGeneratedSerializerEntryProviderForSupplementary({{class.className}}_SERIAL_NAME, () => [{{class.className}}, new {{class.className}}SerializerFactory()]);
{{else}}
{{! 单例注册 }}
registerAutoGeneratedSerializerEntryProviderForSupplementary({{class.className}}_SERIAL_NAME, () => [{{class.className}}, {{class.className}}Serializer.INSTANCE]);
{{/if}}
{{/if}}